//index.js
import regeneratorRuntime from '../../../regenerator-runtime/runtime.js';
var t = getApp(),
  a = t.requirejs("core"),
  c = t.requirejs("api/index"),
  e = (t.requirejs("icons"), t.requirejs("wxParse/wxParse"));//对图片进行处理
const myAudio = wx.createInnerAudioContext();
Page({
  data: {
    route: "index",
    menu: t.tabBar,
    menu_show:false,
    indicatorDots: true, 
    autoplay: true,
    version:t.version,
    interval: 5000,
    duration: 500,
    circular: true,
    icons: t.requirejs("icons"),
    total: 0,
    page: 1,
    show:false,
    display: false,
    hotimg: "/yb_mingpian/static/images/hotdot.jpg",
    notification: "/yb_mingpian/static/images/notification.png",
    background: t.config.background,
    page_bg_img:'',
    page_bg_color: '',
    showtabbar:false,
    tabbar_index: 0,
    show_chat: 0,
    markers: [{
      iconPath: "/yb_mingpian/static/images/red_position_icon.png",
      title:'',
      // latitude: 34.6284500000,
      // longitude: 112.4282100000,
      latitude: null,
      longitude: null,
      width: 50,
      height: 50,
      callout: {
        content: '公司位置',
        company: '',
        color: '#000',
        fontSize: 14,
        borderRadius: 5,
        display: 'ALWAYS',
        textAlign: 'center',
        padding: 10,
        // borderWidth:1,
        // borderColor:"#ddd",
        bgColor:'#fff'
      }
    }],
    video: false,
    config: t.config,
    is_copyright:false,//版权不显示
    bookData:{},//清空表单数据用
    //万能表单
    default_pic: '/yb_mingpian/static/images/add_pic.jpg',
    form: [],
    card_id: 0,
    get_phone: true,
    data: {},
    page_type:3,
    isPlaying: false,
    isPlay: false,
    nowtime: "00:00",
    currecttime: 0,
    totalTime: '',
    timechanging: false,
    audioIndex: -1,
    page_info: {},
    openImg: false,
  },

  //跳转+收集表单id
  formSubmit: function (k) {
    console.log('ddaa',k);
    var e = {},
     vv = { currentTarget: { dataset: {} } },
      data = k.detail.value;
    vv.currentTarget.dataset = data;
    a.menu_url(vv, 1);
    e.formid = k.detail.formId;
    e.openid = getApp().getCache("userinfo").openid;
    e.username = getApp().getCache("userinfo").nickName;
    a.get('Market/getFormid', e, function (t) {
      console.log(t)
    })
  },
  onHide: function () {
    myAudio.destroy();
    if (this.data.page_type == 3) {
      t.setCache('back_menu', 'index', 2);
    }
  },
  //跳转
  to_url: function (k) {
    console.log('ddaawwww', k);
    a.menu_url(k, 1);
  },
  // //跳转
  // to_url:function(k){

  //    var data=a.pdata(k),
  //      url = data.url ? data.url:'',
  //   appid = data.appid ? data.appid : '',
  //      path = data.path ? data.path : '',
  //   title = data.title ? data.title : '';
  //   c.to_url(data.key, url, appid,path,title);
  // },

  //底部导航跳转
  menu_url: function (k) {
    console.log("99999999999999999999",k);
    a.menu_url(k,2);
  },
    async onLoad(options) {
      // myaudio.onPause(function () {
      //   console.log('mmp')
      //   wx.showToast({
      //     title: 'heheda',
      //   })
      // })
 
    var res = await getApp().getExtC();
    var cache = t.getCache("userinfo");
      var that = this;
      if (!cache.nickName && !cache.avatarUrl) {
        getApp().authSwitch(function(res){
          if (res == 1) {
            that.setData({
              display: true
            })
          } else {
            that.setData({
              display: false
            })
          }
        })
      }

    if (options &&  options.pid) {
      t.setCache('pid', options.pid);
    }
    if (options && options.card_id) {
      t.globalData.card_id = options.card_id;
    }

      t.set_caidan(function () {
        a.setting();
        that.setData({
          config: getApp().config
        })
      });
      var e = t.getCache("userinfo");
      if (e) {
        that.setData({
          display: false
        })
      }
      that.getList();

},//88558

  //留言提交
  formBook:function(e){
    var auth_res = this.authCheck();
    if (!auth_res) {
      return;
    }

    var that=this,
    data=e.detail.value;
    if (data.name==''){
      a.error('姓名不能为空');
      return;
    }else if(data.phone.length!=11){
      a.error('手机号格式不正确');
      return;
    }
    // else if (data.booktime == '') {
    //   a.error('预约时间不能为空');
    //   return;
    // }
    data.user_id = getApp().getCache("userinfo").uid;
    a.get('Index/WriteBook',data,function(t){
      if(t.code==0){
        that.setData({
          bookData:{}
        });
        a.success('提交成功');
      }else{
        a.alert(t.msg);
      }
    })
   
  },
  /**
   * 监听开始时间
   */
  listenerTime: function (e) {
    //调用setData()重新绘制
    this.setData({
      "bookData.book_time": e.detail.value,
    });
  },
  
  onShow: function () {
    var that = this;
      t.set_caidan();
      t.get_news_num(that);
      that.setData({
      card_id: t.globalData.card_id
    })
    var e = t.getCache("userinfo");
    if (e) {
      t.userlog(1, '官网', 'website_browse');
    }
    var back_menu = t.getCache('back_menu');

    if (that.data.page_type != 3 && back_menu) {
        that.getList();
    }
  },

 getList:function(){
   var that = this;
   c.indexMod(that, function (t) {
     console.log("yyyy",t.index_page_name)
     that.setData(t);
     wx.setNavigationBarTitle({
       title: t.index_page_name ? t.index_page_name : "官网"
     })
     that.setData({
       page_info: t.page
     });

     // 开屏弹窗广告
     var showopenimg = getApp().getCache('index_open_img_show');
     var open_img_url = that.data.page_info.open_img.imgurl;
     if ((open_img_url != undefined && open_img_url != '') && (showopenimg == undefined || showopenimg == '')) {
       that.setData({
         openImg: true
       });
     } else {
       that.setData({
         openImg: false
       });
     }
   })
 },

  // 关闭开屏弹窗广告
  openImg_close:function(){
    this.setData({
      openImg: false
    });
    // 半天弹1次
    t.setCache('index_open_img_show', true, 3600*12);
  },

  to_zhaopin:function(e){
    var that=this,
    id=a.pdata(e).id;
    if(id){
      a.jump('/yb_mingpian/pages/zhaopin/index?id=' + id);
    }
  },
  /**
 * 下拉刷新
 */
  onPullDownRefresh: function () {
    this.onLoad();
    wx.stopPullDownRefresh();
  },

  /**
   * 地图
   */
  navigate: function (e) {
    var t = a.pdata(e);
    if (t.name && t.lat && t.lng) {
      a.tx_map(t.lat, t.lng, t.name, this.data.markers[0].callout.company)
    } else {
      a.toast('不能获取到该位置')
    }
  },

  phone: function (t) {
    a.phone(t)
  },
  
 // 万能表单提交begin//
  formPower: function (e) {
    c.formSubmit(this, e);
  },
  bindPickerChange: function (e) {
    c.bindPickerChange(this, e);
  },
  listen_time_two: function (e) {
    c.listen_time_two(this, e);
  },
  chooseImageTap1: function (e) {
    var id = a.pdata(e).id;
    c.upload(this, id, 'form_pic', 1)
  },
  chooseImageTap2: function (e) {
    var id = a.pdata(e).id;
    c.upload(this, id, 'form_pic', 0)
  },
  Image_del: function (e) {
    c.Image_del(this, e);
  },
  onGotUserInfo: function (q) {
    var that = this,
      e = t.getCache("userinfo");
    if (e) {
      // that.setData({
      //   display: false
      // })
      // return;
    }
    t.getUserInfo(q.detail.userInfo, function (t) {
      if (t != 1000) {
        var ucache = getApp().getCache("userinfo");
        if (ucache) {
          that.setData({
            display: false,
            getPhoneAuth: getApp().globalData.getphone
          })
        }
      } else {
        that.setData({
          display: true
        })
      }

    }, q.detail.encryptedData, q.detail.iv);
  },

  // 授权获取用户信息判断
  authCheck: function () {
    var cache = getApp().getCache("userinfo");
    if (!cache.nickName && !cache.avatarUrl) {
      this.setData({
        display: true
      })
      return false;
    } else {
      this.setData({
        display: false
      })
      return true;
    }
  },

  // 取消登录
  cancel_auth: function () {
    this.setData({
      display: false
    })
  },

  // 获取用户手机号
  getMobile: function (e) {
    t.globalData.getphone = false;
    this.setData({
      getPhoneAuth: false
    })
    t.getPhoneNumber(e, this, 3);
  },

   // 万能表单提交end//
  /**
   * 图片预览
   */
  previewImage:function(e){
   var url=a.pdata(e).url,
   arr=a.pdata(e).arr;
   a.previewImage(url, arr,'imgurl')
  },
 
 //长按版权展示
  copyright:function(){
    var that=this,
      i = that.data.is_copyright;
      that.setData({
        is_copyright:i?false:true
      })
    
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (e) {
    var that = this.data,
      uid = getApp().getCache('userinfo').uid ? getApp().getCache('userinfo').uid : 0;

    t.userlog(2, '官网', 'website_relay');

    return {
     // title: that.name,
      path: '/yb_mingpian/pages/index/index?pid=' + uid + "&card_id=" + t.globalData.card_id,
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },
  to_chat: function () {
    a.jump('/yb_mingpian/pages/chats/chats');
  },
  getPhoneNumber: function (e) {
    t.getPhoneNumber(e, this, 1);
  },

  //音频组件播放
  play_status_change: function (e) {
    var audioSrc = a.pdata(e).src;
    var audioIndex = a.pdata(e).index;
    var that = this;
    myAudio.src = audioSrc;

    if (that.data.audioIndex == audioIndex){
      if (that.data.isPlay) {
        myAudio.pause();
        that.setData({isPlay: false})
      } else {
        myAudio.play();
        that.setData({isPlay: true});
      }
    } else {
      if (that.data.audioIndex >= 0) {
        myAudio.pause();
        that.setData({
          currecttime: 0,
          nowtime: "00:00",
          audioIndex: audioIndex
        });
        myAudio.play();
      } else {
        that.setData({
          audioIndex: audioIndex,
          isPlay: true
        })
        myAudio.play();
      }
    }

    myAudio.onPlay(function (res) {
      console.log("onPlay !!!!!!!!!");
      that.setData({
        isPlay: true
      })
    })

    myAudio.onError(function (res) {
      console.log("onError !!!!!!!!!",res);
    })

    myAudio.onEnded(function (res) {
      console.log("onEnded !!!!!!!!!");
      that.setData({
        currecttime: 0,
        isPlay: false,
        nowtime: "00:00"
      });
      //myAudio.destroy()
    })
    myAudio.onPause(function (res) {
      console.log("onPause !!!!!!!!!");
      that.setData({
        isPlay: false,
      })
    })
    myAudio.onTimeUpdate(function (res) {
      console.log("onTimeUpdate !!!!!!!!!");
      console.log("currentTime",parseInt(myAudio.currentTime));
      console.log("duration",parseInt(myAudio.duration));
      if (!that.data.timechanging) {
        that.setData({
          currecttime: parseInt(myAudio.currentTime),
          nowtime: that.timeformat(parseInt(myAudio.currentTime)),
          duration: parseInt(myAudio.duration),
          totalTime: that.timeformat(parseInt(myAudio.duration)),
        })
      }
    })
  },

  // 视频播放
  video: function (event) {
    var index = event.currentTarget.dataset['index'];

    if (!this.data.videoIndex) { // 没有播放时播放视频
      this.setData({
        videoIndex: index
      })
      var videoContext = wx.createVideoContext('video' + index)
      videoContext.play()
    } else {
      //停止正在播放的视频
      var videoContextPrev = wx.createVideoContext('video' + this.data.videoIndex)
      videoContextPrev.stop()
      //将点击视频进行播放
      this.setData({
        videoIndex: index
      })
      var videoContextCurrent = wx.createVideoContext('video' + index)
      videoContextCurrent.play()
    }
  },

  timeformat: function (time) {
    var m = parseInt(time / 60);
    m = m < 10 ? "0" + m : "" + m;
    var s = time % 60;
    s = s < 10 ? "0" + s : "" + s;
    return m + ":" + s;
  },
  timechanging: function (res) {
    console.log("timechanging !!!!!!!!");
    this.setData({
      timechanging: true,
    })
  },
  timechange: function (res) {
    console.log("timechange !!!!!!!!");
    var time = res.detail.value;
    myAudio.seek(time);

    this.setData({
      currecttime: time,
      nowtime: this.timeformat(time),
      timechanging: false,
    })
    myAudio.play();
  },
  onUnload: function () {
    myAudio.destroy()
  },

})
